using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Dracham.TileManagement.CharacterManagement;
using Dracham.Network;
using System.Net.Sockets;
using System.Threading;
using System.Xml;
using System.Net;
using Dracham.Classes;

namespace Dracham
{
    public partial class frmLogin : Form
    {
        #region properties 

        public String Username
        {
            get { return this.txtUsername.Text; }
        }

        public String Password
        {
            get { return this.txtPassword.Text; }
        }

        #endregion

        public frmLogin()
        {
            InitializeComponent();

            Thread loadingThread = new Thread(LoadServers);
            loadingThread.Start();
        }

        public void AddServer(Server server)
        {
            lstServers.Items.Add(server);
            lstServers.SelectedIndex = 0;
        }

        public void LoadServers()
        {
            WebClient  wc = new WebClient();
            String servers = wc.DownloadString(AppSettings.Default.ServersUrl);
            XmlDocument xdoc = new XmlDocument();
            xdoc.LoadXml(servers);

            foreach (XmlNode serverNode in xdoc.GetElementsByTagName("Server"))
            {
                Int32 port = Convert.ToInt32( serverNode.Attributes["Port"].Value );
                String ip = serverNode.Attributes["IP"].Value;
                String name = serverNode.Attributes["Name"].Value;

                this.Invoke((MethodInvoker)delegate()
                {
                    AddServer(new Server(name, ip, port));
                });
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            Settings.RunningSettings.Server = (Server)lstServers.SelectedItem;

            this.DialogResult = DialogResult.OK;
        }
    }
}